package com.zg.scf.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zg.common.core.R;
import com.zg.common.core.User;
import com.zg.common.web.Req;
import com.zg.scf.enm.UserTypeEnum;
import com.zg.scf.domain.req.*;
import com.zg.scf.domain.vo.ScfUserVo;
import com.zg.scf.service.ScfUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"用户管理"})
@RestController
@RequiredArgsConstructor
public class ScfUserController {

    private final ScfUserService scfUserService;

    @ApiOperation("查询用户类型")
    @GetMapping("/user/listType")
    public R<List<UserTypeEnum>> listType() {
        User user = Req.getCurrUser();
        if (UserTypeEnum.SYSTEM.getCode().equals(user.getType())) {
            List<UserTypeEnum> list = Arrays.stream(UserTypeEnum.values()).collect(Collectors.toList());
            return R.success(list);
        } else {
            UserTypeEnum userTypeEnum = UserTypeEnum.getByCode(user.getType());
            return R.success(Collections.singletonList(userTypeEnum));
        }
    }

    @ApiOperation("新增用户")
    @PostMapping("/user/add")
    public R<Void> add(@RequestBody @Valid ScfUserAddReq req) {
        scfUserService.add(req);
        return R.success();
    }

    @ApiOperation("修改用户")
    @PostMapping("/user/edit")
    public R<Void> edit(@RequestBody @Valid ScfUserEditReq req) {
        scfUserService.edit(req);
        return R.success();
    }

    @ApiOperation("删除用户")
    @PostMapping("/user/remove")
    public R<Void> remove(@RequestBody List<Long> idList) {
        scfUserService.remove(idList);
        return R.success();
    }

    @ApiOperation("分页查询用户")
    @PostMapping("/user/page")
    public R<IPage<ScfUserVo>> page(@RequestBody @Valid ScfUserPageReq req) {
        IPage<ScfUserVo> page = scfUserService.page(req);
        return R.success(page);
    }

    @ApiOperation("用户注册")
    @PostMapping("/un/user/register")
    public R<Void> register(@RequestBody @Valid ScfUserRegisterReq req) {
        scfUserService.register(req);
        return R.success();
    }
}
